csharp System.ArraySegment<T>类(方法)实例源码

下面列出了csharp System.ArraySegment<T> 类(方法)源码代码实例,从而了解它的用法。

作者:.NET开发    项目:Syste   
//引入命名空间
using System;

public class SamplesArray  {
 
   public static void Main()  {
 
      // Create and initialize a new string array.
      String[] myArr = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };
 
      // Display the initial contents of the array.
      Console.WriteLine( "The original array initially contains:" );
      PrintIndexAndValues( myArr );

      // Define an array segment that contains the entire array.
      ArraySegment<String> myArrSegAll = new ArraySegment<String>( myArr );

      // Display the contents of the ArraySegment.
      Console.WriteLine( "The first array segment (with all the array's elements) contains:" );
      PrintIndexAndValues( myArrSegAll );

      // Define an array segment that contains the middle five values of the array.
      ArraySegment<String> myArrSegMid = new ArraySegment<String>( myArr, 2, 5 );

      // Display the contents of the ArraySegment.
      Console.WriteLine( "The second array segment (with the middle five elements) contains:" );
      PrintIndexAndValues( myArrSegMid );

      // Modify the fourth element of the first array segment myArrSegAll.
      myArrSegAll.Array[3] = "LION";

      // Display the contents of the second array segment myArrSegMid.
      // Note that the value of its second element also changed.
      Console.WriteLine( "After the first array segment is modified, the second array segment now contains:" );
      PrintIndexAndValues( myArrSegMid );
   }
 
   public static void PrintIndexAndValues( ArraySegment<String> arrSeg )  {
      for ( int i = arrSeg.Offset; i < (arrSeg.Offset + arrSeg.Count); i++ )  {
         Console.WriteLine( "   [{0}] : {1}", i, arrSeg.Array[i] );
      }
      Console.WriteLine();
   }

   public static void PrintIndexAndValues( String[] myArr )  {
      for ( int i = 0; i < myArr.Length; i++ )  {
         Console.WriteLine( "   [{0}] : {1}", i, myArr[i] );
      }
      Console.WriteLine();
   }
}

作者:.NET开发    项目:Syste   
//引入命名空间
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class Example
{
   private const int segmentSize = 10;
   
   public static async Task Main()
   {
      List<Task> tasks = new List<Task>();

      // Create array.
      int[] arr = new int[50];
      for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
         arr[ctr] = ctr + 1;

      // Handle array in segments of 10.
      for (int ctr = 1; ctr <= Math.Ceiling(((double)arr.Length)/segmentSize); ctr++) {
         int multiplier = ctr;
         int elements = (multiplier - 1) * 10 + segmentSize > arr.Length ?
                         arr.Length - (multiplier - 1) * 10 : segmentSize;
         ArraySegment<int> segment = new ArraySegment<int>(arr, (ctr - 1) * 10, elements);
         tasks.Add(Task.Run( () => { IList<int> list = (IList<int>) segment;
                                     for (int index = 0; index < list.Count; index++)
                                        list[index] = list[index] * multiplier;
                                   } ));
      }
      try {
         await Task.WhenAll(tasks.ToArray());
         int elementsShown = 0;
         foreach (var value in arr) {
            Console.Write("{0,3} ", value);
            elementsShown++;
            if (elementsShown % 18 == 0)
               Console.WriteLine();
         }
      }
      catch (AggregateException e) {
         Console.WriteLine("Errors occurred when working with the array:");
         foreach (var inner in e.InnerExceptions)
            Console.WriteLine("{0}: {1}", inner.GetType().Name, inner.Message);
      }
   }
}

作者:.NET开发    项目:Syste   
//引入命名空间
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      String[] names = { "Adam", "Bruce", "Charles", "Daniel", 
                         "Ebenezer", "Francis", "Gilbert", 
                         "Henry", "Irving", "John", "Karl",
                         "Lucian", "Michael" };
      var partNames = new ArraySegment<String>(names, 2, 5);
      
      // Cast the ArraySegment object to an IList<String> and enumerate it.
      var list = (IList<String>) partNames;
      for (int ctr = 0; ctr <= list.Count - 1; ctr++)
         Console.WriteLine(list[ctr]);
   }
}


问题


面经


文章

微信
公众号

扫码关注公众号